//
//  LeakeTableViewController.swift
//  testShare
//
//  Created by LiangJianle on 15/9/18.
//  Copyright (c) 2015年 LiangJianle. All rights reserved.
//

import Foundation
import ImageLoader

class LeakeTableViewController: UITableViewController,UIActionSheetDelegate {
    
    var dataList:[TimelineModel] = [TimelineModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.tableView.backgroundColor = UIColor.whiteColor()
        self.navigationController?.navigationBar.barTintColor = UIColor(red: 59/255.0, green: 59/255.0, blue: 59/255.0, alpha: 59/255.0)
        
        ///分享按钮
        let shareButton = UIButton(frame: CGRectMake(0, 0, 20, 20))
     //   shareButton.setTitle("分享", forState: UIControlState.Normal)
        shareButton.setImage(UIImage(named: "share_nor"), forState: UIControlState.Normal)
        shareButton.addTarget(self, action: "shareAction:", forControlEvents: UIControlEvents.TouchUpInside)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: shareButton)


        
        self.tableView.estimatedRowHeight = 300
        self.tableView.rowHeight = UITableViewAutomaticDimension
        
        initData()
    }
    
    
    func initData(){
        var timeline = TimelineModel()
        timeline.createTime = 1439988566
        timeline.userHeadImageURL = "http://img1.2345.com/duoteimg/qqTxImg/2013/12/ka_3/04-054658_103.jpg"
        timeline.userName = "Esports海涛"
        timeline.content = "昨天发了一个错误的简历范本的图如下。很多朋友留言说，错误的范本知道了，那么正确的范本是什么呢？因为大学还没毕业就到上海工作了，所以我其实没经历过招聘会，那么招聘会式的简历范本我不太专业，但我也."
        
        dataList.append(timeline)
        dataList.append(timeline)
        dataList.append(timeline)
        dataList.append(timeline)
        dataList.append(timeline)
        dataList.append(timeline)
        dataList.append(timeline)
        dataList.append(timeline)
        
        self.tableView.reloadData()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - Table view data source
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return dataList.count;
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell: UITableViewCell?
        
        if indexPath.section == 0{
            var testTableViewCell = tableView.dequeueReusableCellWithIdentifier("leakeTableViewCell", forIndexPath: indexPath) as! LeakeTableViewCell
            

            var model = dataList[indexPath.row]
            
            testTableViewCell.userHeadImageView.load(model.userHeadImageURL!)
            
            testTableViewCell.userNameLabel.text = model.userName
            testTableViewCell.createTimeLabel.text = NSDate(timeIntervalSince1970: model.createTime!).timeAgoSinceNow()
          //  testTableViewCell.contentLabel.preferredMaxLayoutWidth =  CGRectGetWidth(tableView.bounds)
            testTableViewCell.contentLabel.numberOfLines = 0
            testTableViewCell.contentLabel.text = model.content
            
            
            testTableViewCell.selectionStyle = UITableViewCellSelectionStyle.None
            return testTableViewCell
            
            
        }
        
        // Configure the cell...
        
        return cell!
    }
    
    
    
    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        return 1
    }
    
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }
    func shareAction(sender: UIButton) {
        
        let actionSheet = UIActionSheet()
        actionSheet.cancelButtonIndex = 0
        actionSheet.addButtonWithTitle("取消")
        actionSheet.addButtonWithTitle("分享到微信好友")
        actionSheet.addButtonWithTitle("分享到朋友圈")
        actionSheet.delegate = self
        actionSheet.showInView(self.view)

    }
    
    func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
        
        var title = "测试标题,为王尼玛点赞，助Ta成为尼玛人气之星"
        var content = "王尼玛助力2015最HIT人物"
        var url = "http://weibo.com/p/1005051781937274"
        switch buttonIndex{
        case 1://分享到微信好友
            ShareRender.shareToWechatFriend(title: title, content: content, url: url)
            break;
        case 2://分享到微信朋友圈
            ShareRender.shareToWeChatTimeline(title: title, url: url)
            break;
        default:
            break
        }
    }
    

    
}

